# Maintainer: Konstantin Gizdov <arch at kge dot pw>
# Maintainer: Torsten Keßler <tpkessler at archlinux dot org>
# Contributor: huyz
# Contributor: Jingbei Li <i@jingbei.li>
pkgname=intel-oneapi-mkl
_pkgver=2023.2.0
_debpkgrel=49495
pkgver=${_pkgver}_${_debpkgrel}
pkgrel=2
pkgdesc="Intel oneAPI Math Kernel Library"
arch=('loong64' 'x86_64')
url='https://software.intel.com/content/www/us/en/develop/tools/oneapi.html'
license=("custom")
source=("https://apt.repos.intel.com/oneapi/pool/main/${pkgname}-${_pkgver}-${_pkgver}-${_debpkgrel}_amd64.deb"
        "https://apt.repos.intel.com/oneapi/pool/main/${pkgname}-devel-${_pkgver}-${_pkgver}-${_debpkgrel}_amd64.deb"
        "https://apt.repos.intel.com/oneapi/pool/main/${pkgname}-common-${_pkgver}-${_pkgver}-${_debpkgrel}_all.deb"
        "https://apt.repos.intel.com/oneapi/pool/main/${pkgname}-common-devel-${_pkgver}-${_pkgver}-${_debpkgrel}_all.deb"
        "${pkgname}.conf"
        "${pkgname}.sh")
depends=('intel-oneapi-compiler-dpcpp-cpp-runtime' 'intel-oneapi-tbb' 'intel-oneapi-common')
replaces=('intel-mkl' 'intel-mkl-static')
provides=('intel-mkl' 'intel-mkl-static')
conflicts=('intel-mkl' 'intel-mkl-static' 'intel-oneapi-basekit')
options=('staticlibs')
noextract=("${pkgname}-${_pkgver}-${_pkgver}-${_debpkgrel}_amd64.deb"
           "${pkgname}-devel-${_pkgver}-${_pkgver}-${_debpkgrel}_amd64.deb"
           "${pkgname}-common-${_pkgver}-${_pkgver}-${_debpkgrel}_all.deb"
           "${pkgname}-common-devel-${_pkgver}-${_pkgver}-${_debpkgrel}_all.deb")
sha256sums=('6d65a60faa308c9faf2224ad79ea3fad16742715fc3d10e53926225b5f28525d'
            '6a9a8bebbaee82d64d052b55f6ccacfbde6d7bc6e18292a6e135e180a5813a95'
            '0202cb8ad6681f47e290f689d142dae9d686e1d772b11f0b6a0f3b5b8bcf7398'
            '3de924a117afcfb7de6174ca338a3d436461ff41243a900449bada986a4b5c22'
            'cd787d9843146ad5ccf28ec72311d76d95d6bb9e42974dd614d8b5aa76bb5bd9'
            '85a56750e03ad99a5b99e7296dcea411fb88c4a0a25e674c86ab48f5648d9ae9')

package() {
    ar x ${pkgname}-${_pkgver}-${_pkgver}-${_debpkgrel}_amd64.deb
    tar xf data.tar.xz -C "${pkgdir}"
    rm data.tar.xz

    ar x ${pkgname}-devel-${_pkgver}-${_pkgver}-${_debpkgrel}_amd64.deb
    tar xf data.tar.xz -C "${pkgdir}"
    rm data.tar.xz

    ar x ${pkgname}-common-${_pkgver}-${_pkgver}-${_debpkgrel}_all.deb
    tar xf data.tar.xz -C "${pkgdir}"
    rm data.tar.xz

    ar x ${pkgname}-common-devel-${_pkgver}-${_pkgver}-${_debpkgrel}_all.deb
    tar xf data.tar.xz -C "${pkgdir}"
    rm data.tar.xz

    # drop conda binaries
    rm -r "${pkgdir}"/opt/intel/oneapi/conda_channel

    # symlink mkl version
    ln -s "$_pkgver" ${pkgdir}/opt/intel/oneapi/mkl/latest

    install -Dm644 ${pkgname}.conf ${pkgdir}/etc/ld.so.conf.d/${pkgname}.conf
    install -Dm644 ${pkgname}.sh ${pkgdir}/etc/profile.d/${pkgname}.sh

    install -d "${pkgdir}"/usr/share/licenses/"${pkgname}"
    ln -s /usr/share/licenses/intel-oneapi "${pkgdir}"/usr/share/licenses/"${pkgname}"/oneapi

    # pkgconfig
    cd ${pkgdir}/opt/intel/oneapi/mkl/latest/lib/pkgconfig
    install -d ${pkgdir}/usr/share/pkgconfig
    for _file in *.pc; do
        ln -s /opt/intel/oneapi/mkl/latest/lib/pkgconfig/${_file} ${pkgdir}/usr/share/pkgconfig/${_file}
        sed -e 's@prefix=.*@prefix=/opt/intel/oneapi/mkl/latest@g' -i ${_file}
    done

    # provide old intel-mkl
    install -d ${pkgdir}/usr/bin
    install -d ${pkgdir}/opt/intel/mkl
    install -d ${pkgdir}/opt/intel/mkl/lib
    ln -sf /opt/intel/oneapi/mkl/latest/bin/intel64 ${pkgdir}/opt/intel/mkl/bin
    ln -sf /opt/intel/oneapi/mkl/latest/lib/intel64 ${pkgdir}/opt/intel/mkl/lib/intel64
    ln -sf /opt/intel/oneapi/mkl/latest/lib/intel64 ${pkgdir}/opt/intel/mkl/lib/intel64_lin
    ln -sf /opt/intel/oneapi/mkl/latest/include ${pkgdir}/opt/intel/mkl/include
    ln -sf /opt/intel/mkl/bin/mkl_link_tool ${pkgdir}/usr/bin/mkl_link_tool
}
